package com.zpark.drug.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.common.ResultMapUtil;
import com.zpark.drug.pojo.Customer;
import com.zpark.drug.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping
    public String login(){
        return "/customer";
    }
    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping(value = "/customerPage")
    public String customerPage (){
        return "/customerPage";
    }
    /**
     * 转向供应商编辑页面
     * @return
     */
    @RequestMapping(value = "/customerQueryById")
    public Object customerQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Customer customer = customerService.findById(id);
        model.addAttribute("obj",customer);
        return "/customerPage";
    }
    /**
     * 获取所有供应商
     * @return
     */
    @RequestMapping(value = "/customerList")
    @ResponseBody
    public Object customerList(){
        List<Customer> customerList = customerService.findAll();
        return ResultMapUtil.getHashMapList(customerList);
    }

    /**
     * 添加一个供应商
     * @return
     */
    @RequestMapping(value = "/customerAdd")
    @ResponseBody
    public Object customerAdd(Customer customer){
        try {
            customer.setTime(new Date());
            int i = customerService.addCustomer(customer);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/customerEdit")
    @ResponseBody
    public Object customerEdit(Customer customer){
        try {
            int i = customerService.updateCustomer(customer);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     * @return
     */
    @RequestMapping(value = "/customerDelById")
    @ResponseBody
    public Object customerDelById(Integer id){
        try {
            int i = customerService.deleteCustomerById(id);

            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 分页查询供应商列表
     * @return
     */
    @RequestMapping(value = "/customerQueryPage")
    @ResponseBody
    public Map<String, Object> customerQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Customer> customerPageInfo = customerService.findPage(page, limit, param);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", customerPageInfo.getTotal());
        map.put("data", customerPageInfo.getList());
        return map;
    }

}
